<?php
namespace Home\Controller;
use Think\Controller;

import("Org.Util.simple_html_dom");
class IndexController extends Controller {
    public function index(){
//        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        exit("ddd");
    }


//通过线路
     public function getLineFromNumber(){
         $pa = json_decode(file_get_contents("php://input"),true);
         $lineid = $pa['lineid'];
         $file_contents = file_get_contents('http://qd.3gclient.com:891/wap/linesearch.aspx?lineid='.$lineid.'&submit=%E6%9F%A5%E8%AF%A2');

         if (!isset($file_contents))
         {
             httpReturn(1,'请检查网络设置',null);
         }else{
             $html = str_get_html($file_contents);
             if (!isset($html)){
                 httpReturn(1,'请检查网络设置',null);
             }else{
                 $items = array();
                 foreach ($html->find('.tdr a') as $element){
                     if (isset($element->style))
                     {
                         $item = getParam($element->href);
                         $item['lineName'] = $element->plaintext;
                         $items[] = $item;
                     }
                 }
                 $data = array(
                   "items" =>$items,
                 );
                 httpReturn(0,'',$data);
             }
         }
     }

     //通过站点名
    public function getLineFromStation(){
        $pa = json_decode(file_get_contents("php://input"),true);
        $siteid = $pa['siteid'];
        $file_contents = file_get_contents('http://qd.3gclient.com:891/wap/sitesearch.aspx?siteid='.$siteid.'&submit=%E6%9F%A5%E8%AF%A2');

        if (!isset($file_contents))
        {
            httpReturn(1,'请检查网络设置',null);
        }else{
            $html = str_get_html($file_contents);
            if (!isset($html)){
                httpReturn(1,'请检查网络设置',null);
            }else{
                $items = array();

                foreach ($html->find('.tdr a') as $element){
//                    httpReturn(3,'',null);
                    $item = getParam($element->href);
                    $item['stationName'] = $element->plaintext;
                    $items[] = $item;
                }
                $data = array(
                    "items" =>$items,
                );
                httpReturn(0,'',$data);
            }
        }
    }
    public function getBusStatus(){
        $pa = json_decode(file_get_contents("php://input"),true);
        $rundir = $pa['rundir'];
        $jeid = $pa['jeid'];

        $file_contents = file_get_contents('http://qd.3gclient.com:891/wap/subline.aspx?rundir='.$rundir.'&id='.$jeid);


        if (!isset($file_contents))
        {
            httpReturn(1,'请检查网络设置',null);
        }else{


            $html = str_get_html($file_contents);
            if (!isset($html)){
                httpReturn(1,'请检查网络设置',null);
            }else{
                $items = array();

                foreach ($html->find('#divSite p') as $element){
//                    httpReturn(3,'',null);
//                    $item = getParam($element->href);
//                    $item['stationName'] = $element->plaintext;
//                    $items[] = $element;
                    $item = array();
                    $a = $element->find('a');
                    if (count($a)){
                        $ac = $a[0];
                        $item = getParam($ac->href);
                        $item['stationName']=$ac->plaintext;
                        $item['busStatus'] = 'ori';
                    }
                    $items[] = $item;
                }

                    $arrives = array();
                foreach ($html->find('#divRoad div') as $divElement){
                     $busStatus = $divElement->class;
                     $lineHeight = explode('px',explode(':',$divElement->style)[1])[0];
                     $num = floor($lineHeight/21.0);

                    $arrayItem = array(
                        'busStatus' => $busStatus,
                        'num' => $num,
                    );
                    $arrives[] = $arrayItem;

                }

                $data = array(
                    "items" =>$items,
                    "arrives"=>$arrives,
                );
                 $LineNameEles =$html->find('#spanLineName');

                if (isset($LineNameEles) && count($LineNameEles)){

                    $data['lineName']=$LineNameEles[0]->plaintext;
                }
                $dirEles = $html->find('#spanDir');
                if (isset($dirEles) && count($dirEles)){

                    $data['dir']=$dirEles[0]->plaintext;
                }
                $timeEles = $html->find('#spanTime');
                if (isset($timeEles) && count($timeEles)){

                    $data['time']=$timeEles[0]->plaintext;
                }
                $lineStateEles = $html->find('#spanLineState');
                if (isset($lineStateEles) && count($lineStateEles)){

                    $data['lineState']=$lineStateEles[0]->plaintext;
                }

                httpReturn(0,'',$data);
            }
        }
    }

}

